package com.stlm2.controller.qrcode;

import com.google.zxing.WriterException;

import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.User;
import com.stlm2.dems.service.UserService;
import com.stlm2.util.QrcodeImageUtils;
import com.stlm2.util.WatermarkUtils;
import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.*;


/**
 * Author：fuchunyu
 * Date：2017/8/3
 * Description：二维码控制器
 */
@Controller
@RequestMapping("/qrcodeC")
public class QrcodeController {
    @Resource
    private UserService userService;
    @Resource
    private UserDao userDao;

    /**
     * Author：fuchunyu
     * Date：2017/8/4
     * Description：获取转帐二维码
     */

    @RequestMapping("/encode/{id}.jpg")
    public void encode(String companyCode, HttpServletResponse response,@PathVariable String id,String childId ){

        OutputStream out=null;
        User user=userService.getById(Integer.parseInt(id));
        String avotorr=user.getAvotorr()!=null?user.getAvotorr():"http://ddm-image.oss-cn-beijing.aliyuncs.com/427509015230495868.jpg";
        try {
            out=response.getOutputStream();
            QrcodeImageUtils.Encode_QR_CODE("id="+user.getId()+",childId="+childId,out,avotorr);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriterException e) {
            e.printStackTrace();
        }
        //QrcodeUtils.encode("id="+id,out);

    }
    /**
     * Author：fuchunyu
     * Date：2017/8/4
     * Description：获取邀请二维码
     */

    @RequestMapping("/encode/{id}.png")
    public void encode( String companyCode,HttpServletResponse response,@PathVariable int id){

        OutputStream out=null;
        User user=userService.getById(id);
        String avotorr=user.getAvotorr()!=null?user.getAvotorr():"http://ddm-image.oss-cn-beijing.aliyuncs.com/427509015230495868.jpg";
        try {
            out=response.getOutputStream();
            QrcodeImageUtils.Encode_QR_CODE("http://www.ddmzl.com/m/html/register.html/?id="+user.getId(),out,avotorr);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

    /**
     * Author：fuchunyu
     * Date：2017/8/4
     * Description：卡券二维码
     */

    @RequestMapping("/encode/{id}.jpeg")
    public void encodeApp(String companyCode, HttpServletResponse response,@PathVariable int id){

        OutputStream out=null;
        String avotorr="http://ddm-image.oss-cn-beijing.aliyuncs.com/427509015230495868.jpg";
        try {
            out=response.getOutputStream();
            QrcodeImageUtils.Encode_QR_CODE("cardId="+1,out,avotorr);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

     }
    public static String[] analyzeJsonToArray(JSONObject jsonject, String type) {

        String string = jsonject.toString();
        string = string.replace("}", "");
        string = string.replace("{", "");
        string = string.replace("\"", "");
        String[] strings = string.split(",");

        if (type.equals("key")) {
            String[] stringsNum = new String[strings.length];
            for (int i = 0; i < strings.length; i++) {
                stringsNum[i] = strings[i].split(":")[0];
            }
            return stringsNum;
        } else if (type.equals("value")) {
            String[] stringsName = new String[strings.length];
            for (int i = 0; i < strings.length; i++) {
                stringsName[i] = strings[i].split(":")[1];
            }
            return stringsName;
        } else {
            return null;
        }
    }
    /**
     * Author：fuchunyu
     * Date：2017/9/26
     * Description：推广海报
     */

    @RequestMapping("/authorize/{id}.jpg")
    public void licenseImg(String companyCode,HttpServletResponse response, @PathVariable String id, HttpServletRequest req) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        String path ="http://image.chinaddcat.com/318544373639435097.jpg";

        User user=userService.getById(Integer.parseInt(id));

        if (user != null ) {
            String grade =  StringUtils.isNotBlank(user.getUserLevel().toString() ) ? user.getUserLevel().toString() : "";
            String cnName = StringUtils.isNotBlank(user.getRealName()) ? user.getRealName().trim() : user.getName();
            String weixin = StringUtils.isNotBlank(user.getWeixin()) ? user.getWeixin() : "";
            String phone = StringUtils.isNotBlank(user.getPhone()) ? user.getPhone() : "";

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(user.getCreateTime());
            String year = String.valueOf(calendar.get(Calendar.YEAR));
            String month = (calendar.get(Calendar.MONTH) + 1) < 10 ? ("0" + String.valueOf(calendar.get(Calendar.MONTH) + 1)) : String.valueOf(calendar.get(Calendar.MONTH) + 1);
            String day = calendar.get(Calendar.DAY_OF_MONTH) < 10 ? ("0" + String.valueOf(calendar.get(Calendar.DAY_OF_MONTH))) : String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
            Map<String, int[]> pressWords = new IdentityHashMap<String, int[]>();

            //打印等级名称 付春雨
            /*if (StringUtils.isNotBlank(grade)) {
                int[] gradeArr = new int[3];
                gradeArr[0] = 13;
                gradeArr[1] = 130;
                gradeArr[2] = 311;
                pressWords.put(grade, gradeArr);
            }*/
            //打印的昵称
            int[] cnNameArr = new int[3];
            cnNameArr[0] = 26;      //字体大小
            cnNameArr[1] = 290;     //x轴坐标
            cnNameArr[2] = 430;     //y轴坐标
            pressWords.put(cnName, cnNameArr);
            //打印的年
            int[] yearArr = new int[3];
            yearArr[0] = 22;      //字体大小
            yearArr[1] = 300;     //x轴坐标
            yearArr[2] = 958;     //y轴坐标
            pressWords.put(year, yearArr);
            //打印的月
            int[] monthArr = new int[3];
            monthArr[0] = 22;      //字体大小
            monthArr[1] = 380;     //x轴坐标
            monthArr[2] = 958;     //y轴坐标
            pressWords.put(month, monthArr);
            //打印的天
            int[] dayArr = new int[3];
            dayArr[0] = 22;      //字体大小
            dayArr[1] = 445;     //x轴坐标
            dayArr[2] = 958;     //y轴坐标
            pressWords.put(day, dayArr);

            //打印的微信
            /*if (StringUtils.isNotBlank(weixin)) {
                int[] weixinArr = new int[3];
                weixinArr[0] = 13;      //字体大小
                weixinArr[1] = 221;     //x轴坐标
                weixinArr[2] = 395;     //y轴坐标
                pressWords.put(weixin, weixinArr);
            }*/

            //打印的联系方式
           /* if (StringUtils.isNotBlank(phone)) {
                int[] phoneArr = new int[3];
                phoneArr[0] = 13;      //字体大小
                phoneArr[1] = 221;     //x轴坐标
                phoneArr[2] = 419;     //y轴坐标
                pressWords.put(phone, phoneArr);
            }*/

            try {
                //获取当前流对象
                OutputStream stream = response.getOutputStream();
                //把信息写到图片上
                Image image= WatermarkUtils.pressLicense(path, pressWords);
                //把二维码写到图片上
                WatermarkUtils.pressQrcode(image, "http://ddt.chinaddcat.com/m/phoneHtml/index.html?userId="+user.getId(), 220, 219, stream, 288, 569);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
